﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.shop
{
    public partial class shop_domain_edit : System.Web.UI.Page
    {
        YXShop.BLL.Shop.Shop shopBll = new YXShop.BLL.Shop.Shop();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("013004004", "对不起，您没有权限进行编辑");
                InitWebControl();
                string shopId = ChangeHope.WebPage.PageRequest.GetQueryString("shopId");
                if(shopId!=null&&shopId!="")
                {
                    ViewState["shopId"] = shopId;
                    YXShop.Model.Shop.Shop shop = shopBll.GetModelById(Convert.ToInt32(shopId));
                    if(shop!=null)
                    {
                        this.lblShopNum.Text = shop.Shopnum.ToString();
                        this.lblTime.Text = shop.Addtime.ToString();
                        this.txtName.Text = shop.Shopname.ToString();
                        this.txtDomain.Text = shop.Twodomainname.ToString();
                        this.rblUse.SelectedValue = shop.Attribute.ToString().Split(',')[5];
                        ViewState["attribute"] = shop.Attribute.ToString().Substring(0,10);
                    }
                }
            }
        }

         #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName,"店铺名称不能为空,建议不修改好让店铺出名","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtDomain,"二级域名不能空","isnull","必填","该项为必填项");
            this.Form.Attributes.Add("onsubmit","return CheckForm()");
        }
       #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {
           if(ViewState["shopId"]!=null)
           {
               YXShop.Model.Shop.Shop model = shopBll.GetModelById(Convert.ToInt32(ViewState["shopId"]));
               model.Shopname = this.txtName.Text.Trim().ToString();
               string twodomainname = this.txtDomain.Text.Trim().ToString();
               model.Twodomainname = twodomainname;
               model.Attribute = ViewState["attribute"].ToString() + this.rblUse.SelectedValue;
               List<YXShop.Model.Shop.Shop> shoplist = shopBll.GetListShopByWhere(" twodomainname= '" + twodomainname + "'");
               if (shoplist != null &&shoplist.Count > 0)
               {
                   if (shoplist.Count > 1)
                   {
                       this.ltlMsg.Text = "操作失败，已经存在该域名";
                       this.pnlMsg.Visible = true;
                       this.pnlMsg.CssClass = "actionErr";
                       return;
                   }
                   else
                   {
                       foreach (YXShop.Model.Shop.Shop item in shoplist)
                       {
                           if (!item.Sid.ToString().Equals(model.Sid.ToString()))
                           {
                               this.ltlMsg.Text = "操作失败，已经存在该域名";
                               this.pnlMsg.Visible = true;
                               this.pnlMsg.CssClass = "actionErr";
                               return;
                           }
                           else
                           {
                               int count=shopBll.Update(model);
                               if (count > 0)
                               {
                                   this.ltlMsg.Text = "操作成功，已经修改信息";
                                   this.pnlMsg.Visible = true;
                                   this.pnlMsg.CssClass = "actionOk";
                               }
                               else
                               {
                                   this.ltlMsg.Text = "操作失败";
                                   this.pnlMsg.Visible = true;
                                   this.pnlMsg.CssClass = "actionErr";
                               }
                               
                           }
                       }
                   }
               }
               else
               {
                   int count=shopBll.Update(model);
                   if (count > 0)
                   {
                       this.ltlMsg.Text = "操作成功，已经修改信息";
                       this.pnlMsg.Visible = true;
                       this.pnlMsg.CssClass = "actionOk";
                   }
                   else
                   {
                       this.ltlMsg.Text = "操作失败";
                       this.pnlMsg.Visible = true;
                       this.pnlMsg.CssClass = "actionErr";
                   }
                   
               }           
           }
        }

    }
}
